<template>
  <div class="login_container">
    <div class="login_box">
      <!-- 头像 -->
      <div class="avator_box">
        <img src="../assets/logo.png" />
      </div>
      <!-- 表单区域 -->
      <el-form ref="loginRef" :model="loginForm" :rules="loginRules" label-width="0px" class="login_form">
        <el-form-item prop="username">
          <el-input v-model="loginForm.username" placeholder="账号" prefix-icon="el-icon-user" clearable></el-input>
        </el-form-item>

        <el-form-item prop="password">
          <el-input v-model="loginForm.password" type="password" placeholder="密码" prefix-icon="el-icon-lock" clearable show-password></el-input>
        </el-form-item>

        <el-form-item class="btns">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="success" @click="regist">注册</el-button>
          <el-button type="danger" @click="resetPwd">忘记密码</el-button>
        </el-form-item>
      </el-form>
    </div>

  </div>
</template>

<script>
export default {
  data () {
    return {
      // 登录表单数据绑定对象
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      // 登录表单验证规则
      loginRules: {
        username: [
          {required: true, message: '请输入用户名', trigger: 'blur'},
          {min: 3, max: 10, message: '用户名长度为3到10个字符', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
          {min: 6, max: 115, message: '密码长度为6到15个字符', trigger: 'blur'}
        ]
      }
    }
  },
  methods: {
    login () {
      this.$refs.loginRef.validate(async valid => {
        if (valid) {
        // 发送登录请求
          const {data: result} = await this.$http.post('login', this.loginForm)
          if (result.meta.status !== 200) {
            // 登录失败
            return this.$message.error('用户名或密码错误')
          }
          // 登录成功
          this.$message.success('登录成功')
          // 1、将登录成功的token保存到客户端的sessionStorage中
          // 1.1项目中其他的交易，必须在登录之后可以访问
          // 1.2token只应在当前网站打开期间生效，sessionStorage的生命周期为当前会话
          window.sessionStorage.setItem('token', result.data.token)
          // 2、通过编程式导航跳转到后台主页，路由是/main
          this.$router.push('/main')
        }
      })
    },
    regist () {

    },
    resetPwd () {

    }
  }
}
</script>

<style lang='less' scoped="scoped">
.login_container{
  /* background-color: #00aaff; */
  background-image: url(../assets/background.jpg);
  background-size: 100% 100%;
  background-repeat: no-repeat;
  height: 100%;
  margin: 0vw;
  padding: 0vw;
  width: 100%;
}

.login_box{
  width: 380px;
  height: 300px;
  background-color: #F0F0F0;
  border-radius: 3px;
  position: absolute;
  left: 55%;
  top: 25%;
  .avator_box{
    height: 80px;
    width: 80px;
    position: absolute;
    left: 50%;
    transform: translate(-50%);
    img{
      width: 100%;
      height: 100%;
    }
  }
}

.login_form{
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 30px;
  box-sizing: border-box;
}

.btns{
  display: flex;
  justify-content: flex-end;
}

</style>
